package org.wjy.post.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.wjy.post.dao.GoodsDao;
import org.wjy.post.other.ListToJson;
import org.wjy.post.service.GoodsService;
import org.wjy.post.service.OrdersService;
import org.wjy.post.service.UserService;
import org.wjy.post.vo.Goods;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

/**     
 * 类名称：LoginAction   
 * 类描述：用户控制器的实现
 * 创建人：anan   
 * 创建时间：2012-12-21 下午11:17:36   
 * 修改人：anan  
 * 修改时间：2012-12-21 下午11:17:36   
 * 修改备注：   
 * @version        
 * */
@Namespace("/post")
//定义商品action
public class BaseAction extends ActionSupport {
	private static final long serialVersionUID = -2266695172069461659L;
	private static final Logger logger = Logger.getLogger(UserAction.class);
	@Resource(name="goodsService")
	private GoodsService goodsService;
	@Resource(name="userService")
	private UserService userService;
	@Resource(name="ordersService")
	private OrdersService ordersService;
	
	private boolean flag;
	private String sort;
	private int rows=0;
	private int page=0;
	private String desc="down";
	private String order="down";
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	//获取货物信息表
	@Action(value = "getgoodslistAction", results = { })
	public void getgoodslist(){
		List list=goodsService.getGoods();
		
		int totalnum=list.size();
		try {			
			Map<String, Object> maps = new HashMap<String, Object>();			
			maps.put("rows", list);
			maps.put("total", totalnum);
			String json = JSON.toJSONString(maps);
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			
		}
	
	}
	
	@Action(value = "getuserlistAction", results = {})
	public void getuserlist(){
		List list=userService.getUsers();
		int totalnum=list.size();
		try {			
			Map<String, Object> maps = new HashMap<String, Object>();			
			maps.put("rows", list);
			maps.put("total", totalnum);
			String json = JSON.toJSONString(maps);
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			
		}
	}
	
	@Action(value = "getorderslistAction", results = {})
	public void getorderslistAction(){
        List list=ordersService.getorders();
        String[] args={"id","orderid","customerNo","customerName","goodsnum","payable","cre_time","status"};
      
        JSONObject datas=ListToJson.listtojson(list, args);
//       JSONObject datas=new JSONObject();
//       JSONArray items=new JSONArray();
//        
//        
//       // Map<String, Object>[] data = new HashMap[list.size()];
//      for(int i=0;i<list.size();i++){     
//    	  Object[] obj=(Object[]) list.get(i);
//    	  JSONObject da=new JSONObject();
//    	  for(int j=0;j<obj.length;j++){
//    		  da.put("id", obj[j]);
//    		  da.put("orderid", obj[2]);
//    		  da.put("customerNo", obj[j]);
//    		  da.put("customerName", obj[2]);
//    		  da.put("goodsnum", obj[j]);
//    		  da.put("payable", obj[2]);
//    		  da.put("cre_time", obj[j]);
//    		  da.put("status", obj[2]);
//    	  }
//    	  items.add(da);
//    	   }
//      datas.put("rows", items);
    // String jsontest = JSON.toJSONString(datas);
   //   System.out.println(jsontest);
     //  System.out.print(data[0].toString());
		int totalnum=list.size();
		try {			
			
			datas.put("total", totalnum);
			String json = JSON.toJSONString(datas);
			System.out.println(json);
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			
		}
	}
	
	
	
}
